//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { invoke } from '@tauri-apps/api/tauri';

export type SHARE_TARGET_TYPE = number;
export const SHARE_TARGET_WORK_PLAN: SHARE_TARGET_TYPE = 0;
export const SHARE_TARGET_DOC: SHARE_TARGET_TYPE = 1;
// export const SHARE_TARGET_BOARD: SHARE_TARGET_TYPE = 2;
export const SHARE_TARGET_TASK: SHARE_TARGET_TYPE = 3;
export const SHARE_TARGET_BUG: SHARE_TARGET_TYPE = 4;
export const SHARE_TARGET_REQUIRE_MENT: SHARE_TARGET_TYPE = 5;
export const SHARE_TARGET_DRAW: SHARE_TARGET_TYPE = 6;

export type ShareInfo = {
    share_id: string;
    project_id: string;
    target_type: SHARE_TARGET_TYPE;
    target_id: string;
    target_name: string;
    expire_time: number;
    share_secret: string;
    share_user_id: string;
    share_display_name: string;
    share_logo_uri: string;
    share_time: number;
};

export type CreateShareRequest = {
    session_id: string;
    project_id: string;
    target_type: SHARE_TARGET_TYPE;
    target_id: string;
    expire_time: number;
};

export type CreateShareResponse = {
    code: number;
    err_msg: string;
    share_id: string;
    share_secret: string;
};

export type RemoveShareRequest = {
    session_id: string;
    project_id: string;
    share_id: string;
};

export type RemoveShareResponse = {
    code: number;
    err_msg: string;
};

export type ListShareRequest = {
    session_id: string;
    project_id: string;
    filter_by_target_type: boolean;
    target_type: SHARE_TARGET_TYPE,
    filter_by_target_id: boolean;
    target_id: string;
    offset: number;
    limit: number;
};

export type ListShareResponse = {
    code: number;
    err_msg: string;
    total_count: number;
    share_info_list: ShareInfo[];
};


export function get_type_name(targetType: SHARE_TARGET_TYPE): string {
    if (targetType == SHARE_TARGET_WORK_PLAN) {
        return "工作计划";
    } else if (targetType == SHARE_TARGET_DOC) {
        return "项目文档";
    } else if (targetType == SHARE_TARGET_DRAW) {
        return "绘图白板";
    } else if (targetType == SHARE_TARGET_TASK) {
        return "项目任务";
    } else if (targetType == SHARE_TARGET_BUG) {
        return "项目缺陷";
    } else if (targetType == SHARE_TARGET_REQUIRE_MENT) {
        return "项目需求";
    }
    return "";
}

// 创建分享
export async function create_share(request: CreateShareRequest): Promise<CreateShareResponse> {
    const cmd = 'plugin:project_share_api|create_share';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<CreateShareResponse>(cmd, {
        request,
    });
}

// 删除分享
export async function remove_share(request: RemoveShareRequest): Promise<RemoveShareResponse> {
    const cmd = 'plugin:project_share_api|remove_share';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveShareResponse>(cmd, {
        request,
    });
}

// 列出分享
export async function list_share(request: ListShareRequest): Promise<ListShareResponse> {
    const cmd = 'plugin:project_share_api|list_share';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListShareResponse>(cmd, {
        request,
    });
}